package com.idconfirmsystem.util;

import java.io.PrintStream;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {
	public static final String DATE_FORMAT_CHINESE_DAY = "yyyy年MM月dd日";
	public static final String DATE_FORMAT_CHINESE_MINUTE = "yyyy年MM月dd日 HH:mm";
	public static final String DATE_FORMAT_CHINESE_SECOND = "yyyy年MM月dd日 HH:mm:ss";
	public static final String DATE_FORMAT_NORMAL_YEAR = "yyyy";
	public static final String DATE_FORMAT_NORMAL_MONTH = "yyyy-MM";
	public static final String DATE_FORMAT_NORMAL_DAY = "yyyy-MM-dd";
	public static final String DATE_FORMAT_NORMAL_MINUTE = "yyyy-MM-dd HH:mm";
	public static final String DATE_FORMAT_NORMAL_SECOND = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_FORMAT_NORMAL_SECOND_no_space = "yyyyMMdd HHmmss";
	public static final String DATE_FORMAT_NORMAL_SECOND_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final String TIME_FORMAT_NORMAL = "HH:mm";
	public static final String TIME_FORMAT_NORMAL_HH = "HH";
	public static final String TIME_FORMAT_NORMAL_MM = "mm";
	public static final String TIME_FORMAT_NORMAL_YYYYMM = "yyyyMM";
	public static final String TIME_FORMAT_NORMAL_yyyyMMddHHmmssSSS = "yyyyMMddHHmmssSSS";

	public static String getCurrentDate() {
		return getDateString(new Date(), "yyyy-MM-dd");
	}

	public static String getCurrentDateByFormat(String format) {
		return getDateString(new Date(), format);
	}

	public static String getCurrentDateTime() {
		return getDateString(new Date(), "yyyy年MM月dd日 HH:mm:ss");
	}

	public static String getCurrentDateTimeSSS() {
		return getDateString(new Date(), "yyyy-MM-dd HH:mm:ss.SSS");
	}

	public static String getDateString(Date date) {
		return getDateString(date, "yyyy-MM-dd");
	}

	public static String getDateTimeString(Date date) {
		return getDateString(date, "yyyy年MM月dd日 HH:mm:ss");
	}

	public static String getDateString(Date date, String format) {
		String ret = "";
		if (format != null)
			ret = new SimpleDateFormat(format).format(date);
		else {
			ret = new SimpleDateFormat("yyyy-MM-dd HH:mm").format(date);
		}
		return ret;
	}

	public static Date stringToDate(String datestr, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			if (datestr == null) {
				return null;
			}
			Date date = sdf.parse(datestr);
			return date;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static Date nextDay(Date d, int next) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(5, c.get(5) + next);
		return new Timestamp(c.getTimeInMillis());
	}

	public static Date nextMonth(Date d, int next) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(2, c.get(2) + next);
		return c.getTime();
	}

	public static Date nextYear(Date d, int next) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(1, c.get(1) + next);
		return c.getTime();
	}

	public static Date nextHour(Date d, int next) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(11, c.get(11) + next);
		return c.getTime();
	}

	public static Date nextMinute(Date d, int next) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(12, c.get(12) + next);
		return c.getTime();
	}

	public static Date nextSecond(Date d, int next) {
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		c.set(13, c.get(13) + next);
		return c.getTime();
	}

	public static List<Date> getBetweenDate(Date date1, Date date2) {
		List ret = new ArrayList();

		long num = getQuot(date1, date2);

		Calendar c = Calendar.getInstance();
		c.setTime(date1.getTime() < date2.getTime() ? date1 : date2);
		ret.add(new Timestamp(c.getTimeInMillis()));
		while (num > 0L) {
			c.set(5, c.get(5) + 1);
			ret.add(new Timestamp(c.getTimeInMillis()));
			num -= 1L;
		}
		return ret;
	}

	public static long getQuot(Date date1, Date date2) {
		long quot = 0L;
		Date b1 = Timestamp.valueOf(getDate2Str(date1, "yyyy-MM-dd")
				+ " 00:00:00.000");
		Date e1 = Timestamp.valueOf(getDate2Str(date2, "yyyy-MM-dd")
				+ " 00:00:00.000");

		quot = b1.getTime() - e1.getTime();
		quot = quot > 0L ? quot : -1L * quot;
		quot = quot / 1000L / 60L / 60L / 24L;
		return quot;
	}

	public static Date getNextWeek(Date dateStart) {
		Calendar c = Calendar.getInstance();
		c.setTime(dateStart);
		c.set(5, c.get(5) + 7);
		return new Timestamp(c.getTimeInMillis());
	}

	public static Date getUpWeek(Date dateStart) {
		Calendar c = Calendar.getInstance();
		c.setTime(dateStart);
		c.set(5, c.get(5) - 7);
		return new Timestamp(c.getTimeInMillis());
	}

	public static String getDate2Str(Date dateEnd, String format) {
		if (format == null)
			format = "yyyy-MM-dd HH:mm:ss";
		DateFormat format1 = new SimpleDateFormat(format);
		return format1.format(dateEnd);
	}

	public static Date getStr2Date(String str, String format) {
		if (format == null)
			format = "yyyy-MM-dd HH:mm:ss";
		DateFormat f1 = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = f1.parse(str);
		} catch (ParseException e) {
			e.printStackTrace();
			System.out.println("字符串转化为日期出错");
		}
		return date;
	}

	public static Date getDateByArray(Integer[] arr) {
		Calendar c = Calendar.getInstance();
		c.set(arr[0].intValue(), arr[1].intValue(), arr[2].intValue(),
				arr[3].intValue(), arr[4].intValue(), arr[5].intValue());
		return c.getTime();
	}

	public static Integer getMonth(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int m = c.get(2);
		return Integer.valueOf(m + 1);
	}

	public static Integer getYear(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int m = c.get(1);
		return Integer.valueOf(m);
	}

	public static Integer getDay(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int d = c.get(5);
		return Integer.valueOf(d);
	}

	public static Integer[] getArrayByDate(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int y = c.get(1);
		int m = c.get(2);
		int d = c.get(5);
		int hh = c.get(11);
		int mm = c.get(12);
		int ss = c.get(13);
		Integer[] ret = { Integer.valueOf(y), Integer.valueOf(m),
				Integer.valueOf(d), Integer.valueOf(hh), Integer.valueOf(mm),
				Integer.valueOf(ss) };
		return ret;
	}

	public static Date getNowTime() {
		return new Timestamp(System.currentTimeMillis());
	}

	public static Integer getWeek(Date dateStart, Date dateEnd) {
		Calendar c = Calendar.getInstance();
		c.setTime(dateStart);
		int week = c.get(3);

		Calendar c2 = Calendar.getInstance();
		c2.setTime(dateEnd);
		int week2 = c2.get(3);

		return Integer.valueOf(week2 - week + 1);
	}

	public static Integer getWeek(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int week = c.get(3);
		return Integer.valueOf(week);
	}

	public static Integer getWeekNum(Date date) {
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		int week = c.get(7);
		return Integer.valueOf(week);
	}

	public static String getWeekNameCN(int week) {
		String[] str = { "日", "一", "二", "三", "四", "五", "六" };
		return str[(week - 1)];
	}

	public static Date[] getNowWeekDate(Date date) {
		return getWeekDate(date, 0);
	}

	public static Date[] getBeforeWeekDate(Date date) {
		return getWeekDate(date, -7);
	}

	public static Date[] getNextWeekDate(Date date) {
		return getWeekDate(date, 7);
	}

	private static Date[] getWeekDate(Date date, int setp) {
		Date[] retd = new Date[7];
		int w = getWeekNum(date).intValue();
		for (int i = 0; i < 7; i++) {
			int t = i - w + 1 + setp;
			Calendar c = Calendar.getInstance();
			c.setTime(date);
			c.set(5, c.get(5) + t);
			retd[i] = new Timestamp(c.getTimeInMillis());
		}
		return retd;
	}

	public static Date[] getNowWeekBoundary(Date date) {
		return getWeekBoundary(date, 0);
	}

	public static Date[] getBeforeWeekBoundary(Date date) {
		return getWeekBoundary(date, -7);
	}

	public static Date[] getNextWeekBoundary(Date date) {
		return getWeekBoundary(date, 7);
	}

	private static Date[] getWeekBoundary(Date date, int step) {
		Date[] retd = new Date[2];
		int w = getWeekNum(date).intValue();

		int t = 0 - w + 1 + step;
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.set(5, c.get(5) + t);
		retd[0] = new Timestamp(c.getTimeInMillis());

		int t1 = 7 - w + 1 + step;
		Calendar c1 = Calendar.getInstance();
		c1.setTime(date);
		c1.set(5, c1.get(5) + t1);
		retd[1] = new Timestamp(c1.getTimeInMillis());

		return retd;
	}

	public static Date getNextTimeStamp(Date now, int hour, int minute) {
		Integer[] arr = getArrayByDate(now);
		arr[3] = Integer.valueOf(hour);
		arr[4] = Integer.valueOf(minute);
		Date after = getDateByArray(arr);
		if (after.getTime() <= now.getTime()) {
			Date newnow = nextDay(now, 1);
			Integer[] arr2 = getArrayByDate(newnow);
			arr2[3] = Integer.valueOf(hour);
			arr2[4] = Integer.valueOf(minute);
			return getDateByArray(arr2);
		}
		return after;
	}

	public static void main(String[] args) {
		Date now = getNowTime();
		Date date = getNextTimeStamp(now, 11, 40);
		System.out.println(getDate2Str(date, null));
		System.out.println(date.getTime() - now.getTime());
	}
}
